<?php

class Validate_CityIsPresent extends Zend_Validate_Abstract
{
	private $type;
	const RECORD_FOUND = 'recordFound';
	
	//типы проверок
	const OBJECT_DELIVERY = 'delivery';
	
	public function __construct($type = self::OBJECT_DELIVERY) {
		$this->type = $type;
	}
	
	protected $_messageTemplates = array(
		self::RECORD_FOUND => 'Данный город не найден'
	);
	
	public function isValid($value)
	{
		$this->_setValue($value);
		
		/* @var $citiesRow CitiesRow */
		$citiesRow = GeoService::getInstance()->getCityByName($value);
		if (!$citiesRow) {
			$this->_error(self::RECORD_FOUND);
		} else {
			switch ($this->type) {
				case self::OBJECT_DELIVERY:
					if (!$citiesRow->getIsDelivery()) {
						$this->_error(self::RECORD_FOUND);
					}
					break;
				default:
					$this->_error(self::RECORD_FOUND);
			}
		}
			
		if (count($this->_messages)) {
            return false;
        } else {
            return true;
        }
	}
}
